//
//  BQNewFeaturesViewController.m
//  BeeQuick
//
//  Created by AaronWei on 2017/7/14.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQNewFeaturesViewController.h"
#import "BQPageControl.h"
#import "BQNewFeaturesCell.h"
#import "BQTabBarController.h"

@interface BQNewFeaturesViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) UICollectionView *collectionView;

@property (weak, nonatomic) UIPageControl *pageControl;

@end

@implementation BQNewFeaturesViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCollectionView];
    
    [self setupPageControll];
    
}

- (void)setupPageControll {
    
    BQPageControl *pageControl = [BQPageControl new];
    
    _pageControl = pageControl;
    
    [pageControl setValue:[UIImage imageNamed:@"icon_dot"] forKey:@"pageImage"];
    
    [pageControl setValue:[UIImage imageNamed:@"icon_selected_dot"] forKey:@"currentPageImage"];
    
    pageControl.numberOfPages = 4;
    
    [self.view addSubview:pageControl];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.offset(-8);
    }];
    
}

- (void)setupCollectionView {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = _ScreenSize.size;
    
    flowLayout.minimumLineSpacing = 0;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    self.collectionView = collectionView;
    
    collectionView.dataSource = self;
    
    collectionView.delegate = self;
    
    [self.view addSubview:collectionView];
    
    [self.collectionView registerClass:[BQNewFeaturesCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.bounces = NO;
    
    UIButton *startButton = [[UIButton alloc] init];
    
    [startButton setBackgroundImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal];
    
    [startButton sizeToFit];
    
    startButton.x = 3 * _ScreenWidth + (_ScreenWidth - startButton.frame.size.width) * 0.5;
    
    startButton.y = _ScreenHeight * 0.85;
    
    [self.collectionView addSubview:startButton];
    
    [startButton addTarget:self action:@selector(clickStartButton) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)clickStartButton {
    
    BQTabBarController *tabBarController = [BQTabBarController new];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarController;
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger page = (_collectionView.contentOffset.x / _collectionView.bounds.size.width + 0.499);
    
    _pageControl.currentPage = page % _pageControl.numberOfPages;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BQNewFeaturesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.image = [UIImage imageNamed:[NSString stringWithFormat:@"guide_40_%zd", indexPath.row + 1]];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
